package com.bsoft.exchange.service;

import com.bsoft.exchange.dao.his.MedicaladviceRosteringMapper;
import com.bsoft.exchange.domain.*;
import com.bsoft.exchange.pojo.RecordsPojo;
import com.bsoft.exchange.util.BaseResponse;
import com.bsoft.exchange.util.Constant;
import com.bsoft.exchange.util.TextUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 创建日期 : 2019/12/17
 * 创建人 : 赵太行
 */
@Service
public class MedicaladviceRosteringService {
    @Autowired
    MedicaladviceRosteringMapper medicaladviceRosteringMapper;
    @Autowired
    PublicService publicService;
    private List<DiagnosisJzhmInfo> diagnosisDoaminList;
    private List<RosteringSsxx> ssxxDomains;

    /**
     * 获取就医记录
     */
    public BaseResponse getRosteringList(RecordsPojo recordsPojo){
        BaseResponse baseResponse=new BaseResponse();
        PageHelper.startPage(recordsPojo.getPageNo(), recordsPojo.getPageSize());
        List<RoseringDomain> roseringDomainList=medicaladviceRosteringMapper.getRosteringList(
                recordsPojo.getOrganizationId(),recordsPojo.getLastModifyTime());
        HashSet<Integer>jzhmList=new HashSet<>();
        HashSet<Integer>ZYHList=new HashSet<>();
        List<DoctorInfo> doctorInfo=publicService.getDoctorInfo(recordsPojo.getOrganizationId());

        for (RoseringDomain roseringDomain:roseringDomainList){
            if (roseringDomain.getRecordsId()!=null){
                jzhmList.add(roseringDomain.getRecordsId());
            }
            if (roseringDomain.getRecordsType()==2){
                ZYHList.add(roseringDomain.getRecordsId());
            }
            for (DoctorInfo doctorInfo1:doctorInfo) {
                if (doctorInfo1.getYGDM().equals(roseringDomain.getYSDM())){
                    roseringDomain.setDoctorName(doctorInfo1.getYGXM());
                }
            }
        }
        if (jzhmList.size()>0) {
            diagnosisDoaminList = medicaladviceRosteringMapper.getDiagnosisInfo(recordsPojo.getOrganizationId(), jzhmList);
        }
        if (ZYHList.size()>0) {
            ssxxDomains = medicaladviceRosteringMapper.getSsxx(recordsPojo.getOrganizationId(), ZYHList);
        }
        for (RoseringDomain roseringDomain:roseringDomainList){
            if (diagnosisDoaminList!=null){
                for (DiagnosisJzhmInfo diagnosisDoamin:diagnosisDoaminList) {
                    if (diagnosisDoamin.getJZHM().equals(roseringDomain.getRecordsId())) {
                        roseringDomain.setDiagnosisInfoList(diagnosisDoamin.getDiagnosisInfoList());
                    }
                }
            }
            if (ssxxDomains!=null) {
                for (RosteringSsxx rosteringSsxx : ssxxDomains) {
                    if (roseringDomain.getRecordsId().equals(rosteringSsxx.getZYH())) {
                        roseringDomain.setOperativeRecordList(rosteringSsxx.getSsxxDomainList());
                    }
                }
            }
        }
        baseResponse.setData(roseringDomainList);
        baseResponse.setCode(Constant.successResponse);
        baseResponse.setMessage("请求成功");
        return baseResponse;
    }
}
